package com.yoomiito.app.ui.my;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.bean.TabBean;
import com.yoomiito.app.ui.anyview.income.IncomeTypeView;
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.widget.TipPop;
import com.yoomiito.app.widget.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public abstract class MyOrderBaseActivity<P extends BaseActPresenter> extends BaseActivity<P> {
    @BindView(R.id.tv_title)
    public TitleView titleView;
    @BindView(R.id.tv_title_dec)
    public TextView desTitleTv;
    @BindView(R.id.tv_money)
    public TextView moneyTv;
    @BindView(R.id.tv_des)
    public TextView desTv;
    @BindView(R.id.tab_layout)
    public TabLayout mTabLayout;
    @BindView(R.id.incomeTypeView)
    IncomeTypeView mIncomeTypeView;
    @BindView(R.id.layout_top)
    LinearLayout topLayout;
    private List<MyOrderBaseFragment> mFragmentList = new ArrayList<>();
    private List<TabBean> mTabList = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_order_base;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        mFragmentList = getAllFragment();
        mTabList = getTabList();

        if (mTabList == null || mTabList.size() <1){
            mTabLayout.setVisibility(View.GONE);
        }

        initViewPager();
    }

    protected abstract List<MyOrderBaseFragment> getAllFragment();

    protected abstract List<TabBean> getTabList();

    private void initViewPager() {
        if (this instanceof MyTodayOrderActivity){

        }else {
            desTitleTv.setText(ResUtil.getString(R.string.my_anticipated_income));
        }
        // 提供自定义的布局添加Tab
        if (mTabList != null){
            for (int i = 0; i < mTabList.size(); i++) {
                View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
                TextView tabText = (TextView) view.findViewById(R.id.tab_content);
                TextView tabNumText = (TextView) view.findViewById(R.id.tab_num);
                TabBean tabBean = mTabList.get(i);
                tabText.setText(tabBean.content);
                if (tabBean.isShowMoney) {
                    tabNumText.setText("(" + tabBean.num + ")");
                    tabNumText.setVisibility(View.VISIBLE);
                } else {
                    tabNumText.setVisibility(View.GONE);
                }
                mTabLayout.addTab(mTabLayout.newTab().setCustomView(view).setTag(tabBean.tag));
            }
        }
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabItemSelected(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        onTabItemSelected(0);
    }

    public void onTabItemSelected(int index) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0, j = mFragmentList.size(); i < j; i++) {
            fragment = mFragmentList.get(i);
            if (fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = mFragmentList.get(index);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fragment_container, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();


        // Tab 选中之后，改变各个Tab的状态
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            View view = mTabLayout.getTabAt(i).getCustomView();
            View tabView = view.findViewById(R.id.tab_view);
            if (i == index) { // 选中状态
                tabView.setVisibility(View.VISIBLE);
            } else {// 未选中状态
                tabView.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setTabNum(String... moneys){
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            View view = mTabLayout.getTabAt(i).getCustomView();
            TextView tabNumText = view.findViewById(R.id.tab_num);
            tabNumText.setText("(" + moneys[i] + ")");
        }
    }



    private void shopTip(View view,String tip,int type){
        TipPop tipPop = new TipPop(this,view,tip);

//        if (mTipView==null){
//            mTipView = LayoutInflater.from(this).inflate(R.layout.small_tip_2,null,false);
//        }
//        TextView tipTv = mTipView.findViewById(R.id.tip);
//        ImageView iv = mTipView.findViewById(R.id.indicatorIv);
//        tipTv.setText(tip);
//        if (mPopupWindow==null){
//            mPopupWindow = new PopupWindow(this);
//            mPopupWindow.setOutsideTouchable(true);
//            mPopupWindow.setContentView(mTipView);
//            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
//        }
//        int[] location = new int[2];
//        view.getLocationOnScreen(location);
//        int viewX = location[0];
//        int viewY = location[1];
//        int viewW = view.getWidth();
//        int viewH = view.getHeight();
////        int popupWindowWidth = DensityUtil.dp2px(190);
//        int x =0,y=0;
////        if (viewX>popupWindowWidth/2){//
////            if ((App.mWidthPx-viewW-viewX)>popupWindowWidth/2){
////                x = viewX+viewW/2-popupWindowWidth/2;
////                y = viewY+ viewH+DensityUtil.dp2px(5);
////            }else {
////
////            }
////        }else {
////
////        }
//        if (type==1){
//            x = viewX+viewW/2-DensityUtil.dp2px(14);
//            y = viewY+ viewH+DensityUtil.dp2px(5);
//            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
//            lp.leftMargin = DensityUtil.dp2px(10);
//            iv.setLayoutParams(lp);
//        }else if (type==2){
//            x = viewX+viewW/2-DensityUtil.dp2px(170);
//            y = viewY+ viewH+DensityUtil.dp2px(5);
//            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
//            lp.leftMargin = DensityUtil.dp2px(166);
//            iv.setLayoutParams(lp);
//        }
//        mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
    }
}
